package cn.edu.nju.freshdelivery.dao.impl;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.nju.freshdelivery.dao.DishDAO;
import cn.edu.nju.freshdelivery.model.Buyer;
import cn.edu.nju.freshdelivery.model.Dish;

/**
 * A data access object (DAO) providing persistence and search support for Dish
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see cn.edu.nju.freshdelivery.dao.impl.Dish
 * @author MyEclipse Persistence Tools
 */

public class DishDAOImpl extends HibernateDaoSupport implements DishDAO {
	private static final Logger log = LoggerFactory
			.getLogger(DishDAOImpl.class);
	// property constants
	public static final String NAME = "name";
	public static final String PRICE = "price";
	public static final String DESCRIPTION = "description";
	public static final String PHOTO = "photo";
	public static final String STAR = "star";
	public static final String CATAGORY = "catagory";
	public static final String SELLER = "seller";

	protected void initDao() {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#save(cn.edu.nju.freshdelivery
	 * .model.Dish)
	 */
	public void save(Dish transientInstance) {
		log.debug("saving Dish instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#delete(cn.edu.nju.freshdelivery
	 * .model.Dish)
	 */
	public void delete(Dish persistentInstance) {
		log.debug("deleting Dish instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(Dish persistentInstance) {
		try {
			this.getHibernateTemplate().update(persistentInstance);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findById(java.lang.Integer)
	 */
	public Dish findById(java.lang.Integer id) {
		log.debug("getting Dish instance with id: " + id);
		try {
			Dish instance = (Dish) getHibernateTemplate().get(
					"cn.edu.nju.freshdelivery.model.Dish", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByExample(cn.edu.nju.
	 * freshdelivery.model.Dish)
	 */
	public List<Dish> findByExample(Dish instance) {
		log.debug("finding Dish instance by example");
		try {
			List<Dish> results = (List<Dish>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByProperty(java.lang.String
	 * , java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Dish instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Dish as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByName(java.lang.Object)
	 */
	public List<Dish> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByPrice(java.lang.Object)
	 */
	public List<Dish> findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByDescription(java.lang
	 * .Object)
	 */
	public List<Dish> findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByPhoto(java.lang.Object)
	 */
	public List<Dish> findByPhoto(Object photo) {
		return findByProperty(PHOTO, photo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByStar(java.lang.Object)
	 */
	public List<Dish> findByStar(Object star) {
		return findByProperty(STAR, star);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#findByCatagory(java.lang.Object
	 * )
	 */
	public List<Dish> findByCatagory(Object catagory) {
		return findByProperty(CATAGORY, catagory);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.freshdelivery.dao.impl.DishDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Dish instances");
		try {
			String queryString = "from Dish";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.DishDAO#merge(cn.edu.nju.freshdelivery
	 * .model.Dish)
	 */
	public Dish merge(Dish detachedInstance) {
		log.debug("merging Dish instance");
		try {
			Dish result = (Dish) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Dish instance) {
		log.debug("attaching dirty Dish instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Dish instance) {
		log.debug("attaching clean Dish instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DishDAO getFromApplicationContext(ApplicationContext ctx) {
		return (DishDAO) ctx.getBean("DishDAO");
	}

	@Override
	public List<Dish> findBySeller(Object seller) {
		return findByProperty(SELLER, seller);
	}
}